//
//  HomeListViewController.m
//  RONGAO
//
//  Created by wei wang on 2017/2/22.
//  Copyright © 2017年 wei wang. All rights reserved.
//

#import "HomeListViewController.h"
#import "HomeheadView.h"
#import "HomeTableHeadView.h"
#import "DoorListViewController.h"
#import "DoorSystemDeviceListViewController.h"
#import "RightViewController.h"
#import "AboutViewController.h"
#import "DoorSystemTabBarController.h"
#import "MyCamera.h"
#import "DoorSystemTabBarController.h"
#import "HouseholdsViewController.h"

@interface HomeListViewController ()<UITableViewDelegate,UITableViewDataSource>

/** headView **/
@property(nonatomic,strong) HomeheadView *headView;

/** tableHeadView **/
@property(nonatomic,strong) HomeTableHeadView *tableHeadView;

/** tableView **/
@property(nonatomic,strong) UITableView *tableView;

/** testImageArray **/
@property(nonatomic,strong) NSArray *arrayImage;

/** urlImage **/
@property(nonatomic,strong) NSArray *menuImgaeArray;

/** titleArray **/
@property(nonatomic,strong) NSArray *titleArray;

/** titleImage **/
@property(nonatomic,strong) NSArray *titleImageArray;

/** <#Description#> **/
@property(nonatomic,strong) MyCamera *toshowcamera;
@end

@implementation HomeListViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self setNavigationStyle];

    [self getUserInfo_block:^(UserInfo *userInfo) {
        
    }];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self masLayoutSubview];
    self.headView.bannerView.localizationImageNamesGroup = self.arrayImage;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(GetPushNotificationToView:) name:@"GetPushNotificationToView" object:nil];

}

- (void)GetPushNotificationToView:(NSNotification*)notification{
    NSDictionary *nameDictionary = [notification userInfo];
    NSString *uid = [nameDictionary objectForKey:@"uid"];
    
    NSLog(@"GetPushNotificationToView -------- notification.uid:%@",
          uid);
    
    self.toshowcamera = nil;
    
    
    for (int m=0; m<[[AppDelegate globalDelegate].camera_list count]; m++) {
        MyCamera *camera = (MyCamera *)[[AppDelegate globalDelegate].camera_list objectAtIndex:m];
        
        NSString *device_uid = @"";
#if defined(P2PWEXVS)
        if ([camera.deviceType isEqualToString:DEV_TYPE_ADD_3_SAFEBOX]) {
            
            
            device_uid = camera.deviceID;
            
        } else {
            device_uid = camera.uid;
            
        }
#else
        device_uid = camera.uid;
#endif
        
        NSLog(@"GetPushNotificationToView uid:%@, m=%d", device_uid, m);
        
        if ([device_uid isEqualToString:uid]) {
            
            self.toshowcamera = camera;
            
#if defined(CALL_MEDIA)
            [self loadingToShow];
#else
            
            BOOL isGo = NO;
#if defined(P2PWEXVS)
            if ([camera.deviceType isEqualToString:DEV_TYPE_ADD_3_SAFEBOX]) {
                
                
                isGo = NO;
            } else {
                isGo = YES;
            }
#else
            isGo = YES;
#endif
            if (isGo) {
                for (int n=0; n<100; n++) {
                    if (camera.sessionState == CONNECTION_STATE_NONE) {
                    } else {
                        break;
                    }
                    
                    NSLog(@"n:%d  camera.sessionState:%ld", n, (long)camera.sessionState);
                    usleep(100*1000);
                }
                
            }
            
            
            
#if defined(P2PVIDEODB)
            [self selectToShowView:camera control:@"start_sound"];
            
#else
            [self setPushViewController];
#endif
            
#endif
            break;
        }
    }
    
    if (nil == self.toshowcamera) {
        NSLog(@"No UID To Show View!!!!!!!");
    }
}

- (void)setPushViewController{
    
    [AppDelegate globalDelegate].cameraModel = self.toshowcamera;
    


    NSMutableArray *list = [[NSMutableArray alloc]initWithArray:self.navigationController.viewControllers];
    
    
    UIViewController *vc = list.lastObject;
    
    if ([vc isKindOfClass:[DoorSystemTabBarController class]]) {
        [list removeObject:vc];

        DoorSystemTabBarController *DoorVc = [[DoorSystemTabBarController alloc] init];
        [self getIsRecvAudioFormat:self.toshowcamera];
        [list addObject:DoorVc];

        self.navigationController.viewControllers = list;

    
    }else{
        DoorSystemTabBarController *DoorVc = [[DoorSystemTabBarController alloc] init];
        
        [self getIsRecvAudioFormat:self.toshowcamera];
        [list addObject:DoorVc];
        self.navigationController.viewControllers = list;
    }
    
    

    
}
- (BOOL)getIsRecvAudioFormat:(MyCamera *)cam {
    BOOL isRecvAudioFormat = NO;
    
#if defined(AUDIO_TO_SUPPORT)
    if (cam) {
        if (cam.sessionState == CONNECTION_STATE_CONNECTED) {
            
            //if ([camera getConnectionStateOfChannel:0] == CONNECTION_STATE_CONNECTED)
            
            isRecvAudioFormat = cam.isRecvAudioFormat;
            
            if (isRecvAudioFormat) {
                
            } else {
                NSLog(@"IOTYPE_USER_IPCAM_GETAUDIOOUTFORMAT_REQ");
                SMsgAVIoctrlGetAudioOutFormatReq *s = (SMsgAVIoctrlGetAudioOutFormatReq *)malloc(sizeof(SMsgAVIoctrlGetAudioOutFormatReq));
                s->channel = 0;
                [cam sendIOCtrlToChannel:0 Type:IOTYPE_USER_IPCAM_GETAUDIOOUTFORMAT_REQ Data:(char *)s DataSize:sizeof(SMsgAVIoctrlGetAudioOutFormatReq)];
                free(s);
            }
        }
    }
    
#endif
    NSLog(@"CameraListForLiveViewController isRecvAudioFormat:%d", isRecvAudioFormat);
    
    
    return isRecvAudioFormat;
}

- (void)setNavigationStyle{
    
    [self setCentImage:[UIImage imageNamed:@"home_logo"]];
    [self setRightButtonImage:[UIImage imageNamed:@"icon_shopping"]];
    [self setLeftButtonImage:[UIImage imageNamed:@"icon_menu"]];
    
}

- (void)pushView{
    
    NSLog(@"认识荣高");
    
    [self.navigationController pushViewController:[AboutViewController new] animated:YES];

}
- (void)onRightButtonClick:(id)sender{
    
    [self.navigationController pushViewController:[RightViewController new] animated:YES];
}
- (void)onLeftButtonClick:(id)sender{
    
    [[[AppDelegate globalDelegate] menuViewController] showLeftViewController];

}
#pragma mark - --------------------------UITableView dataSource--------------------------

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 4;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        cell.textLabel.textColor = [UIColor colorWithHexString:@"#56504f"];
        [cell.textLabel setFont:[UIFont systemFontOfSize:15.f]];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }
    
    cell.imageView.image = [UIImage imageNamed: self.titleImageArray[indexPath.row]];
    cell.textLabel.text = self.titleArray[indexPath.row];

    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 56;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    _tableHeadView = [[HomeTableHeadView alloc] init];
    _tableHeadView.backgroundColor = [UIColor whiteColor];
    return _tableHeadView;
}

#pragma mark - --------------------------UITableView delegate--------------------------

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 3)
    {
        HouseholdsViewController *vc = [HouseholdsViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if (indexPath.row == 2)
    {
        DoorListViewController *doorVC = [DoorListViewController new];
        [self.navigationController pushViewController:doorVC animated:YES];
    }
    else
    {
        DoorSystemDeviceListViewController *vc = [DoorSystemDeviceListViewController new];
        vc.indexView = indexPath.row;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        __weak UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [superView addSubview:_tableView];
        //        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;//隐藏tableView的分割线
        //        _tableView.scrollEnabled = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
    }
    
    return _tableView;
    
}


- (HomeheadView *)headView{
    
    if (!_headView) {
        
        __weak UIView *superView = self.view;
        _headView = [[HomeheadView alloc] init];
        [_headView.pushBtn addTarget:self action:@selector(pushView) forControlEvents:UIControlEventTouchUpInside];
        [superView addSubview:_headView];
    }
    return _headView;
}

- (NSArray *)menuImgaeArray{
    
    if (_menuImgaeArray == nil) {
        
        _menuImgaeArray = @[
                            @"https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a4b3d7085dee3d6d2293d48b252b5910/0e2442a7d933c89524cd5cd4d51373f0830200ea.jpg",
                            @"https://ss0.baidu.com/-Po3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a41eb338dd33c895a62bcb3bb72e47c2/5fdf8db1cb134954a2192ccb524e9258d1094a1e.jpg",
                            @"http://c.hiphotos.baidu.com/image/w%3D400/sign=c2318ff84334970a4773112fa5c8d1c0/b7fd5266d0160924c1fae5ccd60735fae7cd340d.jpg"
                            ];
        
    }
    
    return _menuImgaeArray;
    
}

- (NSArray *)arrayImage{
    
    if (!_arrayImage) {
        
        _arrayImage = [NSArray arrayWithObjects:@"home_bg",@"bg_31",@"bg_71", nil];
    }
    return _arrayImage;
}

- (NSArray *)titleImageArray{
    
    if (!_titleImageArray) {
        
        _titleImageArray = @[@"icon_door_load",@"icon_door_load_98",@"icon_door",@"icon_lock"];
    }
    
    return _titleImageArray;
}
- (NSArray *)titleArray{
    
    if (!_titleArray) {
        
        _titleArray = @[@"家居入户门智能系统(有屏)",@"  家居入户门智能系统(无屏)",@"平移门智能系统",@"入户门智能门锁"];
        
    }
    return _titleArray;
}

- (void)masLayoutSubview{
    
    __weak UIView *superView = self.view;
    __weak typeof(self) weakself = self;
    
    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(superView.mas_left);
        make.top.mas_equalTo(superView.mas_top);
        make.right.mas_equalTo(superView.mas_right);
        make.height.mas_equalTo(pxScaleHeight(600));
        
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(weakself.headView.mas_bottom);
        make.right.mas_equalTo(superView.mas_right);
        make.left.mas_equalTo(superView.mas_left);
        make.bottom.mas_equalTo(superView.mas_bottom);
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
